package bidgetserver;

import java.io.IOException;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import emailModule.emailSender;

import bidgetserver.Reminder;
import bidgetserver.PMF;

public class addSearchReminder extends HttpServlet{
	private static final Logger log = Logger.getLogger(addSearchReminder.class.getName());

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {

		//reading parameters from JSP form:
		String searchString = req.getParameter("SearchString");
		String userName= req.getParameter("UserName");;
		String minPrice=req.getParameter("minPrice");
		String maxPrice=req.getParameter("maxPrice");
		String top5Results = req.getParameter("top5results");
		String ID = req.getParameter("ID");
		boolean flagRem = false;

		Reminder reminder = new Reminder(searchString, userName,minPrice, maxPrice, top5Results);
		PersistenceManager pmng = PMF.get().getPersistenceManager();
		
		if(!ID.equals("initialID")){
			reminder.setReminderID(ID);
			flagRem = true;
		}
		
		try{
			pmng.makePersistent(reminder);
		}
		finally{
			pmng.close();
		}
		

		if(flagRem == false){
			return;
		}
		//MAIL BEGIN
		//mail sending to client:
		String MsgBodyString = "Dear <B>" +userName+"</B>,"+"<br><br>";
		MsgBodyString+="A new search result for <B>"+"\""+searchString+"\""+"</B> just popped up in eBay."+"<br>";
		MsgBodyString+="To view the new auction available please check out your Bidget bidding widget."+"<br><br>";
		MsgBodyString+="Thank you,"+"<br>";
		MsgBodyString+="Bidget Team.";
		
		String BidGetLogoPicStr="<img src='http://img199.imageshack.us/img199/5971/bidgetlogo3.png'>";
		String ContactUsStr="Contact us at:<br><B>auction.notifier@gmail.com</B>";
		
		MsgBodyString+="<table><tr></tr><td>____________________________________________________________</td><tr><td>"+BidGetLogoPicStr+"</td></tr><tr><td>";
		MsgBodyString+=ContactUsStr;
		MsgBodyString+="</td></tr></table>";
		
		emailSender.SendEmail(userName, 
				"A new search result just popped up - BidGet automatic notification", MsgBodyString);
		
		//MAIL END
		
	}
}


